package com.lwl.blue.commont.utils;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>  </p>
 *
 * @Date: 2021/11/23 14:01
 * @version: V1.0
 * @Decription:
 * @author: 蓝文良
 */
@Slf4j
public class EasyExcelUtils {
    /**
     * 普通导出
     *
     * @param response
     * @param fileName
     * @param clazz
     * @param exportList
     * @throws IOException
     */
    public static void export(HttpServletResponse response, String fileName, Class clazz, List exportList) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String rFileName = URLEncoder.encode(fileName, "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + rFileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), clazz).sheet(fileName).doWrite(exportList);
    }

    /**
     * 根据模板导出
     * 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
     * 填充list 的时候还要注意 模板中{.} 多了个点 表示list
     *
     * @param response
     * @param fileName
     * @param templatePath
     * @param clazz
     * @param exportList
     * @throws IOException
     */
    public static void exportTemplate(HttpServletResponse response, String fileName, String templatePath, Class clazz, List exportList) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String rFileName = URLEncoder.encode(fileName, "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + rFileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), clazz).withTemplate(templatePath).sheet(fileName).doWrite(exportList);
    }

    /**
     * 根据url 字符串转换成 URL
     *
     * @param photoString
     * @return
     */
    public static URL getURL(String photoString) {
        if (StringUtils.isEmpty(photoString)) {
            return null;
        }
        InputStream netFileInputStream = null;
        URL photo;
        try {
            photoString = photoString.substring(0, photoString.indexOf(";"));
            String fileName = photoString.substring(photoString.lastIndexOf("/") + 1);
            fileName= URLEncoder.encode(fileName, "UTF-8");
            photoString = photoString.substring(0, photoString.lastIndexOf("/")+1)+fileName;
            photo = new URL(photoString);
            if (null != photo) {
                URLConnection urlConn = photo.openConnection();
                netFileInputStream = urlConn.getInputStream();
                if (null == netFileInputStream) {
                    photo = null;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("图片不存在rul{}：", photoString);
            return null;
        } finally {
            try {
                if (netFileInputStream != null)
                    netFileInputStream.close();
            } catch (IOException e) {
            }
        }
        return photo;
    }
}
